﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.OracleClient;

namespace SP_XHWeb.Layouts.SP_XHWeb
{
    public partial class username : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string username =string.Empty;
                string tempname = SPContext.Current.Web.CurrentUser.LoginName;
                username = tempname.Contains("\\") ? tempname.Substring(tempname.IndexOf("\\") + 1) : tempname;
                if (string.IsNullOrEmpty(username))
                {
                    string tempname2 = SPControl.GetContextWeb(this.Context).CurrentUser.LoginName;
                    username = tempname2.Contains("\\") ? tempname2.Substring(tempname2.IndexOf("\\") + 1) : tempname2;
                }
                Response.Write(username);
            }
            catch
            {
                Response.Write("");
            }

            //Response.Write("<br/>"+GetCount());
        }

        public string GetCount()
        {
            string count = "0";
            string OracleConnectionString = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.3.9.14)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=oradb)));User ID=kd;Password=kd;Unicode=True";
            string OracleSelectString = "select count(*) from eas.ct_view_workflow";
            string username = string.Empty;
            try
            {
                string tempname = SPControl.GetContextWeb(this.Context).CurrentUser.LoginName;
                username = tempname.Contains("\\") ? tempname.Substring(tempname.IndexOf("\\") + 1) : tempname;
                using (OracleConnection connection = new OracleConnection(OracleConnectionString))
                {
                    OracleCommand command = connection.CreateCommand();
                    if (!string.IsNullOrEmpty(username))
                    {
                        command.CommandText = OracleSelectString + " where receiver='" + username + "'";
                    }
                    else
                    {
                        string tempname2 = SPContext.Current.Web.CurrentUser.LoginName;
                        username = tempname2.Contains("\\") ? tempname2.Substring(tempname2.IndexOf("\\") + 1) : tempname2;
                        command.CommandText = OracleSelectString + " where receiver='" + username + "'";
                    }
                    connection.Open();
                    OracleDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        count = reader[0].ToString();
                    }
                    reader.Close();
                }
            }
            catch
            {
                count = "0";
            }

            return count;
        }
        
    }
}
